package com.xiaoyu.tool.handles.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaoyu.tool.constant.WxKefuKey;
import com.xiaoyu.tool.entity.kefu.KefuNews;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoyu on 2018/7/4
 * 客服消息封装
 */
public class WxHandlerApi {

    /**
     * 点击事件,图文消息mpnews封装,media_id;文本消息两种情况
     **/
    protected void clickKefuMessage(KefuNews kefuNews, WxMpXmlMessage wxMessage, WxMpService wxMpService) throws WxErrorException {
        WxMpKefuMessage wxMpKefuMessage = new WxMpKefuMessage();
        wxMpKefuMessage.setMsgType(kefuNews.getMsgType());
        wxMpKefuMessage.setToUser(wxMessage.getFromUser());
        sendMessageAction(kefuNews, wxMpService, wxMpKefuMessage);
    }

    private static void sendMessageAction(KefuNews kefuNews, WxMpService wxMpService, WxMpKefuMessage wxMpKefuMessage) throws WxErrorException {
        if (StringUtils.isEmpty(JSON.parseObject(kefuNews.getContant()).getString(WxKefuKey.CONTENT))) {
            //图文消息 media_id
            wxMpKefuMessage.setMpNewsMediaId(JSON.parseObject(kefuNews.getContant()).getString(WxKefuKey.MEDIA_ID));
        } else {
            //文本消息  content
            wxMpKefuMessage.setContent(JSON.parseObject(kefuNews.getContant()).getString(WxKefuKey.CONTENT));
        }
        wxMpService.getKefuService().sendKefuMessage(wxMpKefuMessage);
    }

    public static void clickKefuMessage(KefuNews kefuNews, String openId, WxMpService wxMpService) throws WxErrorException {
        WxMpKefuMessage wxMpKefuMessage = new WxMpKefuMessage();
        wxMpKefuMessage.setMsgType(kefuNews.getMsgType());
        wxMpKefuMessage.setToUser(openId);
        sendMessageAction(kefuNews, wxMpService, wxMpKefuMessage);
    }


    /**
     *用户发送文本消息,获取消息事件(三种情况) articles  content  media_id
     **/
    protected void clickNewsKefuMessage(KefuNews kefuNews, WxMpXmlMessage wxMessage, WxMpService wxMpService) throws WxErrorException {
        //三种情况
        if (StringUtils.isNotEmpty(JSON.parseObject(kefuNews.getContant()).getString(WxKefuKey.ARTICLES))){
            //articles
            List<WxMpKefuMessage.WxArticle> articles = new ArrayList<>();
            JSONArray jsonArray = JSON.parseObject(kefuNews.getContant()).getJSONArray(WxKefuKey.ARTICLES);
            for (int i = 0 ; i < jsonArray.size() ; i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                WxMpKefuMessage.WxArticle wxArticle = new WxMpKefuMessage.WxArticle();
                wxArticle.setTitle(jsonObject.getString(WxKefuKey.TITLE));
                wxArticle.setPicUrl(jsonObject.getString(WxKefuKey.PICURL));
                wxArticle.setUrl(jsonObject.getString(WxKefuKey.URL));
                wxArticle.setDescription(jsonObject.getString(WxKefuKey.DESCRIPTION));
                articles.add(wxArticle);
            }
            sendArticleAction(kefuNews, wxMessage, wxMpService, articles);
        }else {
            clickKefuMessage(kefuNews,wxMessage,wxMpService);
        }
    }

    private void sendArticleAction(KefuNews kefuNews, WxMpXmlMessage wxMessage, WxMpService wxMpService, List<WxMpKefuMessage.WxArticle> articles) throws WxErrorException {
        WxMpKefuMessage wxMpKefuMessage = new WxMpKefuMessage();
        wxMpKefuMessage.setArticles(articles);
        wxMpKefuMessage.setMsgType(kefuNews.getMsgType());
        wxMpKefuMessage.setToUser(wxMessage.getFromUser());
        wxMpService.getKefuService().sendKefuMessage(wxMpKefuMessage);
    }


    /**
     * 关注事件特殊处理发送图文消息
     * */
    protected void clickReplaceNewsKefuMessage(String bargainOrderNo,KefuNews kefuNews,
                                               WxMpXmlMessage wxMessage, WxMpService wxMpService) throws WxErrorException {
        List<WxMpKefuMessage.WxArticle> articles = new ArrayList<>();
        JSONArray jsonArray = JSON.parseObject(kefuNews.getContant()).getJSONArray(WxKefuKey.ARTICLES);
        for (int i = 0 ; i < jsonArray.size() ; i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            WxMpKefuMessage.WxArticle wxArticle = new WxMpKefuMessage.WxArticle();
            wxArticle.setTitle(jsonObject.getString(WxKefuKey.TITLE));
            wxArticle.setUrl(jsonObject.getString(WxKefuKey.URL).replace("?",bargainOrderNo));
            wxArticle.setPicUrl(jsonObject.getString(WxKefuKey.PICURL));
            wxArticle.setDescription(jsonObject.getString(WxKefuKey.DESCRIPTION));
            articles.add(wxArticle);
        }
        sendArticleAction(kefuNews, wxMessage, wxMpService, articles);
    }


    /**
     * 发送客服消息领取优惠券
     * */
    public static void sendGetCardMessage(String msgType,String cardId,String openId,WxMpService wxMpService) throws WxErrorException {
        WxMpKefuMessage wxMpKefuMessage = new WxMpKefuMessage();
        wxMpKefuMessage.setMsgType(msgType);
        wxMpKefuMessage.setToUser(openId);
        wxMpKefuMessage.setCardId(cardId);
        wxMpService.getKefuService().sendKefuMessage(wxMpKefuMessage);
    }


    /**
     * 发送优惠卷预约信息
     * */
    public static void sendCardAppoint(String content,String openId,WxMpService wxMpService) throws WxErrorException {
        WxMpKefuMessage wxMpKefuMessage = new WxMpKefuMessage();
        wxMpKefuMessage.setMsgType(WxConsts.KefuMsgType.TEXT);
        wxMpKefuMessage.setToUser(openId);
        wxMpKefuMessage.setContent(content);
        wxMpService.getKefuService().sendKefuMessage(wxMpKefuMessage);
    }


    /**
     * 发送核销卡卷通知
     * 重复领取卡卷处理
     * */
    public static void consumeMessage(String openId,WxMpService wxMpService) throws WxErrorException {
        WxMpKefuMessage wxMpKefuMessage = new WxMpKefuMessage();
        wxMpKefuMessage.setToUser(openId);
        wxMpKefuMessage.setMsgType(WxConsts.KefuMsgType.TEXT);
        wxMpKefuMessage.setContent("您已经通过商城卡包领取此张优惠,\n此次领取无效!");
        wxMpService.getKefuService().sendKefuMessage(wxMpKefuMessage);
    }

    /**
     * 自动客服文本消息
     * */
    public static void sendTextMessage(String openId,WxMpService wxMpService,String content) throws WxErrorException {
        WxMpKefuMessage wxMpKefuMessage = new WxMpKefuMessage();
        wxMpKefuMessage.setToUser(openId);
        wxMpKefuMessage.setMsgType(WxConsts.KefuMsgType.TEXT);
        wxMpKefuMessage.setContent(content);
        wxMpService.getKefuService().sendKefuMessage(wxMpKefuMessage);
    }






}
